package examination;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AccountServlet
 * @author ChangingSelf
 */
@WebServlet("/AccountServlet")
public class AccountServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AccountServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接收参数
		request.setCharacterEncoding("UTF-8");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		
		String loginUrl = "login.jsp";//登录页面
		String indexUrl = "index.jsp";//主页
		
		if(username==null || password==null) {
			//如果参数不全则跳转回去
			response.sendRedirect(loginUrl);
			return;
		}
		
		AccountBean account = new AccountBean();
		boolean success = account.login(username, password);
		
		if(success) {
			//如果登录成功，则将account对象写入session
			request.getSession().setAttribute("account", account);
			response.sendRedirect(indexUrl);
			return;
		}
		else {
			response.sendRedirect(loginUrl);
			return;
		}
		
	}

}
